package com.zhiqianbao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.activity.Big_PicDialogActivity;
import com.zhiqianbao.entity.ActivityList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

//import com.bumptech.glide.Glide;

/**
 * Created by 子龙 on 2017/7/21.
 * 活动
 */

public class ActivityAdapter extends BaseAdapter {
    List<ActivityList.DataBean.CrmActivityListBean> activityInfoList;
    Context mContext;
    private LayoutInflater inflater;
    private String image_url;


    public ActivityAdapter(Context context){
        this.activityInfoList =  new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
    }


    public void setData(List<ActivityList.DataBean.CrmActivityListBean> data) {
        activityInfoList.addAll(data);
    }

    public void cleadate(){
        activityInfoList.clear();
    }
    @Override
    public int getCount() {
        return activityInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return activityInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null){
            viewHolder = new ViewHolder();
             convertView=inflater.inflate(R.layout.activity_item,null);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        image_url = activityInfoList.get(position).getActivityUrl();
        String activityPicUrl = activityInfoList.get(position).getActivityPicUrl();
        final int id = activityInfoList.get(position).getId();
        Picasso.with(mContext).load(activityPicUrl).into(viewHolder.iv);
            viewHolder.tv.setText(activityInfoList.get(position).getActivityContent());
        viewHolder.iv.setTag(activityInfoList.get(position));
        viewHolder.iv.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                ActivityList.DataBean.CrmActivityListBean bean= (ActivityList.DataBean.CrmActivityListBean) v.getTag();
                Intent intent = new Intent(mContext,Big_PicDialogActivity.class);
                intent.putExtra("checkweb",bean.getActivityUrl());
                intent.putExtra("shareImgUrl",bean.getActivityPicUrl());
                intent.putExtra("shareContent",bean.getActivityContent());
                intent.putExtra("shareTitle",bean.getActivityName());
                intent.putExtra("activityid",id);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder{
        ImageView iv;
        TextView  tv;
    }
}
